using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;
using System.Media;

namespace CuthluralSwitcher
{
    public partial class MainForm : Form
    {
        private const string WALLFOLDER = @"C:\Users\ctouzel\Documents\CuthluralMagnitizerFiles\";
        private const string DEFAULTPIC = @"C:\Windows\System32\oobe\info\backgrounds\backgroundDefault.jpg";
        private const int WIDTH = 1280;
        private const int MIN_HEIGHT = 1050;
        private const int MAX_HEIGHT = 9999;
        private Random rnd = null;

        public MainForm()
        {
            InitializeComponent();
            mainTimer.Enabled = true;
            minTimer.Enabled = true;
            rnd = new Random();
        }


        private void ChangeWallpaper()
        {
            DateTime current = DateTime.Now;
            int curmonth = rnd.Next(3);
            switch (curmonth)
            {
                case 0:
                    current = DateTime.Now.AddDays(15);
                    break;
                case 1:
                    current = DateTime.Now.AddDays(-15);
                    break;
            }

            string folderTag = current.Month.ToString();
            if (current.Day < 15)
            {
                folderTag += "A";
            }
            else
            {
                folderTag += "B";
            }

            string completePath = WALLFOLDER + folderTag;
            if (Directory.Exists(completePath) == false)
            {
                Directory.CreateDirectory(completePath);
            }

            DirectoryInfo dirInfo = new DirectoryInfo(completePath);
            FileInfo[] wallpapers = dirInfo.GetFiles("*.jpg");
            int wallCount = wallpapers.Length;

            FileInfo chosen = wallpapers[rnd.Next(wallCount)];
            CFileInfo info = new CFileInfo(chosen.FullName);

            System.Drawing.Image image = System.Drawing.Image.FromFile(chosen.FullName);



            Image resizedImage = ImageManager.ResizeImage(image, Screen.PrimaryScreen.Bounds.Width, MAX_HEIGHT, Screen.PrimaryScreen.Bounds.Height, false);
            //Image textedImage = ImageManager.AddImageInformationText(resizedImage, info);
            File.Delete(DEFAULTPIC);

            Bitmap bm = (Bitmap)resizedImage;
            Bitmap bmpCrop = bm.Clone(new Rectangle(0, 0, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height), bm.PixelFormat);
            if (bm.Height > Screen.PrimaryScreen.Bounds.Height)
            {
                int diff = bm.Height - Screen.PrimaryScreen.Bounds.Height;
                bmpCrop = bm.Clone(new Rectangle(0, diff / 2, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height), bm.PixelFormat);
            }


            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
            ImageCodecInfo ici = null;
            foreach (ImageCodecInfo codec in codecs)
            {
                if (codec.MimeType == "image/jpeg")
                    ici = codec;
            }

            EncoderParameters ep = new EncoderParameters();
            long currCompr = 90;
            long max = 245 * 1024;
            long currSize = 999999999999;
            while (currSize > max)
            {
                ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, currCompr);
                currCompr -= 5;
                bmpCrop.Save(DEFAULTPIC, ici, ep);
                FileInfo f = new FileInfo(DEFAULTPIC);
                currSize = f.Length;
            }
            WallpaperManager.SetWallpaper(resizedImage);

            info = null;
            image.Dispose();
            image = null;
            resizedImage.Dispose();
            resizedImage = null;
            GC.Collect();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            ChangeWallpaper();
            GC.Collect();
        }

        private void MainForm_Resize(object sender, EventArgs e)
        {
            if (FormWindowState.Minimized == WindowState)
            {
                Hide();
            }
        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            Show();
            WindowState = FormWindowState.Normal;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void changeWallpaperToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ChangeWallpaper();
        }

        private void showWindowToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Show();
            WindowState = FormWindowState.Normal;
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void mainTimer_Tick(object sender, EventArgs e)
        {
            ChangeWallpaper();
        }

        private void btnMinimize_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
            Hide();
        }

        private void minTimer_Tick(object sender, EventArgs e)
        {
            minTimer.Enabled = false;
            minTimer.Dispose();
            minTimer = null;
            this.WindowState = FormWindowState.Minimized;
            Hide();
        }

        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            notifyIcon1.Visible = false;
        }


    }
}